package com.glela.packet.service.impl;

import com.glela.common.constant.ClientType;
import com.glela.common.util.DateUtil;
import com.glela.domain.vo.PerRedPacketVo;
import com.glela.packet.mapper.RedPacketOwnerBizMapper;
import com.glela.packet.mapper.RedPacketOwnerMapper;
import com.glela.packet.model.RedPacketOwner;
import com.glela.packet.service.RedPacketOwnerService;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;






@Service("redPacketOwnerService")
public class RedPacketOwnerServiceImpl implements RedPacketOwnerService {
  

  @Autowired
  private RedPacketOwnerBizMapper redPacketOwnerBizMapper;
  @Autowired
  private RedPacketOwnerMapper redPacketOwnerMapper;


  @Override
  public boolean batchSaveRedPacketOwner(List<RedPacketOwner> list) {
    if (CollectionUtils.isEmpty(list)) {
      return false;
    }
    return redPacketOwnerBizMapper.batchInsert(list) > 0;
  }

  @Override
  public boolean batchUpdateRedPacketOwner(List<RedPacketOwner> list) {
    if (CollectionUtils.isEmpty(list)) {
      return false;
    }
    return redPacketOwnerBizMapper.batchUpdate(list) > 0;
  }

  @Override
  public RedPacketOwner getRedPacketOwnerById(int id) {
    return redPacketOwnerMapper.selectByPrimaryKey(id);
  }

  @Override
  public boolean updateRedPacketOwner(RedPacketOwner redPacketOwner) {
    return redPacketOwnerMapper.updateByPrimaryKeySelective(redPacketOwner) > 0;
  }

	@Override
	public boolean updateRedPacketOwnerUseTime(RedPacketOwner redPacketOwner) {
		return redPacketOwnerBizMapper.updateRedPacketOwnerUseTime(redPacketOwner) > 0;
	}

	@Override
  public RedPacketOwner getByRedPacketShareHistId(int redPacketShareHistId) {
    return redPacketOwnerBizMapper.getByRedPacketShareHistId(redPacketShareHistId);
  }

  @Override
  public boolean saveRedPacketOwner(RedPacketOwner redPacketOwner) {
    return redPacketOwnerMapper.insertSelective(redPacketOwner) > 0;
  }

  @Override
  public List<RedPacketOwner> listWillInvalidRedPacketByUserId(int userId) {
    return redPacketOwnerBizMapper.listWillInvalidRedPacket(userId);
  }

  @Override
  public RedPacketOwner getRedPacketOwner(int userId, short redPacketStatus, int lastOwnerId) {
    return redPacketOwnerBizMapper.findRedPacketOwner(userId,redPacketStatus,lastOwnerId);
  }

  @Override
	public List<PerRedPacketVo> showRedPacketList(Long userId, int type, Integer startIndex, Integer pageSize,String clientType) {
	  Date nowDate = DateUtil.parse(DateUtil.getNow());	 
	  List<PerRedPacketVo> showRedPacketList = redPacketOwnerBizMapper.showRedPacketList(userId,type,nowDate);
		 if (null != showRedPacketList || showRedPacketList.size() != 0) {
				for (PerRedPacketVo perRedPacketVo : showRedPacketList) {
		//	赠送类型：0：分享退回；1：分享领取 ; 2：系统赠送；3：邀请人赠送；4：新用户奖励			
					if (0 == perRedPacketVo.getGiftType()) { // 左下角
						perRedPacketVo.setUserType("退回时间");
						perRedPacketVo.setTitle("分享退回兔币");
					}
					if (1 == perRedPacketVo.getGiftType()) {		//1：分享领取
						perRedPacketVo.setUserType("赠送人");
						perRedPacketVo.setTitle("小铺店主赠送的兔币");
					}
					if (2 == perRedPacketVo.getGiftType()) {   //系统正送
						perRedPacketVo.setUserType("获得时间");
						
					}
					if (4 == perRedPacketVo.getGiftType()) {
						perRedPacketVo.setUserType("新用户");
						perRedPacketVo.setTitle("邀请新用户获得兔币");
					}
					if (3 == perRedPacketVo.getGiftType()) {		//4：新用户奖励	
						perRedPacketVo.setTitle("新店主创业奖励兔币");
						perRedPacketVo.setUserType("邀请人");		// 新用户 显示的 昵称对不对 
					}
					if (type == 0) { // 红包状态为 未使用时 
						if(clientType != null){
							if (clientType == ClientType.H5) {
								if (perRedPacketVo.getRoleId() >= 3) { // H5 店主 去分享
									perRedPacketVo.setRedPacketShareStatus(8);
								}
								if (perRedPacketVo.getRoleId() < 3) { // H5 小白 去使用
									perRedPacketVo.setRedPacketShareStatus(9);
								}
							}
							if (clientType == ClientType.APP) { // app 去分享
								perRedPacketVo.setRedPacketShareStatus(8);
							}
						}
					}
					//1：已分享     4：已消费
					if(type == 1){		//已使用
						if(2 == perRedPacketVo.getGiftType() || 0 == perRedPacketVo.getGiftType()){			// 已使用  并且是系统赠送  分享退回  使用情况 是 分享
							if( 1 == perRedPacketVo.getRedPacketShareStatus()){
								perRedPacketVo.setUserType("分享时间");
								perRedPacketVo.setOwnTime(perRedPacketVo.getShareTime());
							}
							if(4 == perRedPacketVo.getRedPacketShareStatus()){
								perRedPacketVo.setUserType("使用时间");
								perRedPacketVo.setOwnTime(perRedPacketVo.getUsedTime());
							}
						}
						//giftType
						// 赠送类型：0：分享退回；1：分享领取 ; 2：系统赠送；3：邀请人赠送；4：新用户奖励
						// status  
						//分享状态：0：未分享；1：已分享;2：分享已使用；3：分享已退回 4：已使用8:去分享9：去使用
						else{
							if( 1 == perRedPacketVo.getRedPacketShareStatus() ||  3 == perRedPacketVo.getRedPacketShareStatus() || 2 == perRedPacketVo.getRedPacketShareStatus()){
								perRedPacketVo.setOwnTime(perRedPacketVo.getShareTime());
							}
							if(4 == perRedPacketVo.getRedPacketShareStatus()){
								perRedPacketVo.setOwnTime(perRedPacketVo.getUsedTime());
							}
						}
						
					}
					if(type == 2){			//已失效  统一显示失效时间
						if(2 == perRedPacketVo.getGiftType() || 0 == perRedPacketVo.getGiftType()){			// 已使用  并且是系统赠送  分享退回  使用情况 是 分享
								perRedPacketVo.setUserType("失效时间");
						}						
						perRedPacketVo.setOwnTime(perRedPacketVo.getEndTime());
					}
				}
		}
	return showRedPacketList;
  }

@Override
public int queryRedPacketSize(Long userId, int type) {
	Date nowDate = DateUtil.parse(DateUtil.getNow());
	return redPacketOwnerBizMapper.queryRedPacketSize(userId,type,nowDate);
}


	
}
